<?php
class category_content_model {
	
	
	static public function add_media_by_clip($task_id,$nns_date){
		$db_r = nn_get_db(NL_DB_READ);
		$db_w = nn_get_db(NL_DB_WRITE);
		$sql="select * from nns_mgtvbk_clip_task where nns_id='$task_id'";
		$clip_task = nl_db_get_one($sql, $db_r);unset($sql);
		if($clip_task==null||empty($clip_task)){
			return false;
		}
		
		
		
		$nns_clip_date = $nns_date;
		
		
		
		
		
		$vod_id = $clip_task['nns_video_id'];
		$vod_index_id = $clip_task['nns_video_index_id'];
		$org_id = $clip_task['nns_org_id'];
		
		$sql = "select nns_name from nns_vod_index where nns_id='$vod_index_id' and nns_deleted!=1";
		$nns_name = nl_db_get_col($sql, $db_r);unset($sql);
		
		$sql = "update nns_mgtvbk_c2_task set nns_status=3,nns_action='destroy'  where nns_type='media' and nns_org_id='$org_id' and nns_src_id='$vod_index_id' and nns_clip_task_id IS NOT NULL and nns_clip_date  IS NOT NULL";
		nl_execute_by_db($sql, $db_w);unset($sql);
		$sql = "update nns_mgtvbk_c2_task set nns_clip_task_id='$task_id',nns_clip_date='$nns_date'  where nns_type='media' and nns_org_id='$org_id' and nns_src_id='$vod_index_id' and ISNULL(nns_clip_task_id) and ISNULL(nns_clip_date)";
		nl_execute_by_db($sql, $db_w);unset($sql);
		
		return true;
		
		
		
		
				
		/*/查询片源ID__HD
		$sql = "select nns_id from nns_vod_media where nns_vod_id='$vod_id' and nns_mode='hd'  and nns_vod_index_id='$vod_index_id'";
		$media = nl_db_get_one($sql, $db_r);unset($sql);
		if(is_array($media)&&$media!=null){
			$data = array(
			'nns_type' => 'media', 
			'nns_name' => $nns_name, 
			'nns_ref_id' => $media['nns_id'], 
			'nns_action' => 'add', 
			'nns_status' => 1, 
			'nns_org_id' => $org_id, 
			'nns_src_id' => $vod_index_id, 
			'nns_clip_task_id'=>$task_id,
			'nns_clip_date'=>$nns_clip_date
			);
			self::add_media_to_task($data);
			
			$sql="update nns_vod_media set nns_status=0   where nns_vod_id='$vod_id' and  nns_vod_index_id='$vod_index_id'";
			nl_execute_by_db($sql,$db_w);
			return true;
		}
		//查询片源ID__STD
		$sql = "select nns_id from nns_vod_media where nns_vod_id='$vod_id' and nns_mode='std'  and nns_vod_index_id='$vod_index_id'";
		$media = nl_db_get_one($sql, $db_r);unset($sql);
		if(is_array($media)&&$media!=null){
			$data = array(
			'nns_type' => 'media', 
			'nns_name' => $nns_name, 
			'nns_ref_id' => $media['nns_id'], 
			'nns_action' => 'add', 
			'nns_status' => 1, 
			'nns_org_id' => $org_id, 
			'nns_src_id' => $vod_index_id, 
			'nns_clip_task_id'=>$task_id,
			'nns_clip_date'=>$nns_clip_date
			);
			self::add_media_to_task($data);
			$sql="update nns_vod_media set nns_status=0  where nns_vod_id='$vod_id' and  nns_vod_index_id='$vod_index_id'";
			nl_execute_by_db($sql,$db_w);
			return true;
		}
		
		
		//查询片源ID__LOW
		$sql = "select nns_id from nns_vod_media where nns_vod_id='$vod_id' and nns_mode='low'  and nns_vod_index_id='$vod_index_id'";
		$media = nl_db_get_one($sql, $db_r);unset($sql);
		if(is_array($media)&&$media!=null){
			$data = array(
			'nns_type' => 'media', 
			'nns_name' => $nns_name, 
			'nns_ref_id' => $media['nns_id'], 
			'nns_action' => 'add', 
			'nns_status' => 1, 
			'nns_org_id' => $org_id, 
			'nns_src_id' => $vod_index_id, 
			'nns_clip_task_id'=>$task_id,
			'nns_clip_date'=>$nns_clip_date
			);			
			self::add_media_to_task($data);
			$sql="update nns_vod_media set nns_status=0  where nns_vod_id='$vod_id' and  nns_vod_index_id='$vod_index_id'";
			nl_execute_by_db($sql,$db_w);
			return true;
		}*/
	}
	
	static public function add_media_to_task($data){
		$db_r = nn_get_db(NL_DB_READ);
		$db_w = nn_get_db(NL_DB_WRITE);
		$sql="select * from nns_mgtvbk_c2_task where nns_type='media' and nns_org_id='{$data['nns_org_id']}' and  nns_src_id='{$data['nns_src_id']}'";
		$c2_task = nl_db_get_one($sql, $db_r);unset($sql);
		if(is_array($c2_task)&&$c2_task!=null){
			$where = "nns_id='{$c2_task['nns_id']}'";
			$data['nns_modify_time'] = date('Y-m-d H:i:s');
			nl_db_update($db_w,'nns_mgtvbk_c2_task',$data,$where);
		}else{
			$data['nns_create_time'] = date('Y-m-d H:i:s');
			$data['nns_id'] =  np_guid_rand();
			nl_db_insert($db_w,'nns_mgtvbk_c2_task',$data);
		}
	}
	
	
	static public function sync_all($nns_vod_id=null){
		$db_w = nn_get_db(NL_DB_WRITE);
		$db_r = nn_get_db(NL_DB_READ);
		
		$sql = "select * from nns_vod where nns_status in (1,2,3)  and  nns_deleted=0 order by nns_show_time limit 50 ";
		if($nns_vod_id!=null){
			$sql = "select * from nns_vod where nns_id='$nns_vod_id'";
		}
		$vod_list = nl_db_get_all($sql, $db_r);
		if (is_array($vod_list) && count($vod_list) > 0) {
			foreach ($vod_list as $vod_value) {
				$depot_info = category_model::__get_category_all($vod_value['nns_id'], 'vod');
				$category = category_model::get_category_id_by_content_all($depot_info['depot_info'], 0);
				if (is_array($category) && count($category) > 0) {
					$re = self::_task_c2_all($category, $vod_value['nns_id'], 'vod');
					if($re===true){
						$sql = "update nns_vod set nns_status=0 where nns_id='{$vod_value['nns_id']}'";
						nl_execute_by_db($sql, $db_w);
					}
				} else {
					i_write_log_core('分类获取失败', 'copy_vod/vod');
				}
			}
		}
		
		$sql = "select nns_vod_index.* from nns_vod_index left join nns_vod on nns_vod_index.nns_vod_id=nns_vod.nns_id 
where nns_vod_index.nns_status in (1,2,3) and nns_vod.nns_status=0 and nns_vod_index.nns_deleted=0 limit 1000";
		if($nns_vod_id!=null){
			$sql = "select * from nns_vod_index where nns_vod_id='$nns_vod_id'";
		}
		$vod_index_list = nl_db_get_all($sql, $db_r);
		if (is_array($vod_index_list) && count($vod_index_list) > 0) {
			foreach ($vod_index_list as $vod_index_value) {
				$depot_info = category_model::__get_category_all($vod_index_value['nns_id'], 'index');
				$category = category_model::get_category_id_by_content_all($depot_info['depot_info'], 0);
				if (is_array($category) && count($category) > 0) {
					$re = self::_task_c2_all($category, $vod_index_value['nns_id'], 'index');
					if($re===true){
						$sql = "update nns_vod_index set nns_status=0 where nns_id='{$vod_index_value['nns_id']}'";
						nl_execute_by_db($sql, $db_w);
					}
				} else {
					i_write_log_core('分类获取失败', 'copy_index/index');
				}
			}
		}
	}
	

	static public function sync() {
		$db_w = nn_get_db(NL_DB_WRITE);
		$db_r = nn_get_db(NL_DB_READ);
		//vod
		$sql = "select * from nns_vod where nns_status in (1,2,3) limit 1";
		$vod_list = nl_db_get_all($sql, $db_r);
		if (is_array($vod_list) && count($vod_list) > 0) {
			$sp_list = sp_model::get_sp_list();
			foreach ($sp_list as $sp_value) {
				foreach ($vod_list as $vod_value) {
					$depot_info = category_model::__get_category_all($vod_value['nns_id'], 'vod');
					$category = category_model::get_category_id_by_content($depot_info['depot_info'], 0, $sp_value['nns_id']);
					//print_r($category);die;
					if (is_array($category) && count($category) > 0) {
						$re = self::_task_c2($category, $vod_value['nns_id'], 'vod', $sp_value['nns_id']);
						if($re===true){
							$sql = "update nns_vod set nns_status=0 where nns_id='{$vod_value['nns_id']}'";
							nl_execute_by_db($sql, $db_w);
						}
					} else {
						i_write_log_core($sp_value['nns_id'] . '分类获取失败', 'copy_vod/' . $sp_value['nns_id'] . '/index');
					}
				}
			}
		} else {
			i_write_log_core('影片数据已经全部同步', 'copy_vod');
		}
	}

	static public function fail_content($data) {
		$db_w = nn_get_db(NL_DB_WRITE);
		$id = np_guid_rand();
		$now_time = date('Y-m-d H:i:s');
		$insertdata = array('nns_id' => $id, 'nns_type' => $data['nns_type'], 'nns_name' => $data['nns_name'], 'nns_ref_id' => $data['nns_id'], 'nns_org_id' => $data['nns_id'], 'nns_action' => $data['nns_action'], 'nns_create_time' => $now_time, 'nns_modify_time' => $now_time, );
		nl_db_insert($db_w, 'nns_mgtvbk_import_fail', $insertdata);
	}
	
	/**
	 * $category 栏目
	 * $new_video_id 视频ID
	 * $type (vod,index,live,playbill)
	 */
	
	static function _task_c2_all($category, $new_video_id, $type){
		$result = array();
		$db_w = nn_get_db(NL_DB_WRITE);
		$db = nn_get_db(NL_DB_READ);
		$src = "";
		$nns_name = '';
		$nns_ref_id_now = '';
		if ($type == 'index') {//分集
			$vod_index_tmp = nl_db_get_one("select nns_name,nns_vod_id,nns_action,nns_id from nns_vod_index where nns_id='{$new_video_id}'", $db);
			$nns_name = $vod_index_tmp['nns_name'];
			$src = $vod_index_tmp['nns_vod_id'];
			$action = $vod_index_tmp['nns_action'];
			$nns_ref_id_now = $vod_index_tmp['nns_id'];
		} elseif ($type == 'vod') {//影片
			$vod_tmp = nl_db_get_one("select nns_name,nns_action,nns_id from nns_vod where nns_id='{$new_video_id}'", $db);
			$nns_name = $vod_tmp['nns_name'];
			$action = $vod_tmp['nns_action'];
			$nns_ref_id_now = $vod_tmp['nns_id'];
		} elseif ($type == 'playbill') {//节目单
			$playbill_tmp = nl_db_get_one("select nns_name,nns_live_id,nns_id from nns_live_playbill_item where nns_id='{$new_video_id}'", $db);
			$nns_name = $playbill_tmp['nns_name'];
			$src = $playbill_tmp['nns_live_id'];
			$nns_ref_id_now = $playbill_tmp['nns_id'];
			$action = '';//TODO
			unset($playbill_tmp);
		} elseif ($type == 'live') {
			$live_tmp = nl_db_get_one("select nns_name,nns_id from nns_live where nns_id='{$new_video_id}'", $db);
			$nns_name = $live_tmp['nns_name'];
			$nns_ref_id_now = $live_tmp['nns_id'];
			$action = '';//TODO
		}
		$id = np_guid_rand();
		$now_time = date('Y-m-d H:i:s');
		
		foreach ($category as  $value) {
			$sql = "select * from nns_mgtvbk_c2_task where nns_type='{$type}' and nns_ref_id='{$new_video_id}'  and nns_org_id='" . $value['nns_org_id'] . "'";
			$result = nl_db_get_one($sql, $db);
			//如果存在就更新任务状态
			if (is_array($result) && !empty($result)) {
				//判断注入状态
				if ($action == 'add') {
					if ($result['nns_status'] == -1 && $result['nns_action'] == 'add') {
						//注入失败，重新注入
						$sql = "update nns_mgtvbk_c2_task set nns_status=1,nns_modify_time='$now_time' where nns_id='{$result['nns_id']}'";
						$re = nl_execute_by_db($sql, $db_w);
						unset($sql);
						if ($re === false) {
							return false;
						}
					}
				}elseif ($action == 'modify') {
					if ($result['nns_status'] == -1 && $result['nns_action'] == 'add') {
						//注入失败或者重新注入
						$sql = "update nns_mgtvbk_c2_task set nns_status=1,nns_modify_time='$now_time' where nns_id='{$result['nns_id']}'";
						$re = nl_execute_by_db($sql, $db_w);
						unset($sql);
						if ($re === false) {
							return false;
						}
					} else {
						//根据c2的任务状态来重置新的状态
						if ($result['nns_status'] == 1 && $result['nns_action'] == 'add') {
							$nns_action = 'add';
							$nns_status = 1;
						} elseif ($result['nns_status'] == 0 && $result['nns_action'] == 'add') {
							$nns_action = 'modify';
							$nns_status = 2;
						} elseif (($result['nns_status'] == 2 || $result['nns_status'] == 0) && $result['nns_action'] == 'modify') {
							$nns_action = 'modify';
							$nns_status = 2;
						} elseif ($result['nns_status'] == 3 && $result['nns_action'] == 'destroy') {
							$nns_action = 'modify';
							$nns_status = 2;
						} elseif ($result['nns_status'] == 0 && $result['nns_action'] == 'destroy') {
							$nns_action = 'add';
							$nns_status = 1;
						}
						//更新
						if ($result['nns_status'] == 0 || $result['nns_status'] == 1 || $result['nns_status'] == 2 || $result['nns_status'] == 3) {
							$sql = "update nns_mgtvbk_c2_task set nns_action='$nns_action' , nns_status=$nns_status ,nns_modify_time='$now_time' where nns_id='{$result['nns_id']}'";
							if ($type == 'vod' || $type == 'index') {
								$sql = "update nns_mgtvbk_c2_task set nns_action='$nns_action' , nns_status=$nns_status ,nns_modify_time='$now_time'  where  nns_type='vod' and nns_ref_id='{$new_video_id}' and nns_org_id='" . $value['nns_org_id'] . "'";
							} elseif ($type == 'live' || $type == 'playbill') {
								$sql = "update nns_mgtvbk_c2_task set nns_action='$nns_action' ,  nns_status=$nns_status ,nns_modify_time='$now_time'  where  nns_type='live' and nns_ref_id='{$new_video_id}' and nns_org_id='" . $value['nns_org_id'] . "'";
							}
							$re = nl_execute_by_db($sql, $db_w);
							unset($sql);
							//不成功就返回false
							if ($re === false) {
								return false;
							}
						}
					}
				}elseif ($action == 'destroy') {
					$re = nl_execute_by_db("update nns_mgtvbk_c2_task set nns_name='" . $nns_name . "',nns_status=3,nns_action='destroy' where nns_id='" . $result['nns_id'] . "'", $db_w);
					if ($re === false) {
						return false;
					}
				}
			}else {
				//如果没有查出来，统一按照新增
				if ($action == 'modify' || $action == 'add') {
					$id = np_guid_rand();
					$data = array('nns_id' => $id, 'nns_type' => $type, 'nns_name' => $nns_name, 'nns_ref_id' => $new_video_id, 'nns_action' => 'add', 'nns_status' => 1, 'nns_create_time' => $now_time, 'nns_modify_time' => $now_time, 'nns_org_id' => $value['nns_org_id'], 'nns_src_id' => $src, );
					$re = nl_db_insert($db_w, 'nns_mgtvbk_c2_task', $data);
					if ($re === false) {
						return false;
					}
				}
			}
			
			
			//将数据添加到nns_mgtvbk_category_content表中
			if ($type == 'vod') {
				foreach ($category as $category_value) {
					$sql = "select * from nns_mgtvbk_category_content where nns_org_id='" . $category_value['nns_org_id'] . "' and nns_video_id='" . $new_video_id . "' and nns_category_id='{$category_value['nns_id']}'  and  nns_video_type='0'";
					//die($sql);
					$result = nl_db_get_one($sql, $db);
					if (empty($result)) {//如果不存在，则添加
						$vod_name = nl_db_get_one("select nns_name,nns_pinyin from nns_vod where nns_id='{$new_video_id}'", $db);
						$id = np_guid_rand();
						$data = array('nns_id' => $id, 'nns_video_id' => $new_video_id, 'nns_video_name' => $vod_name['nns_name'], 'nns_create_time' => $now_time, 'nns_modify_time' => $now_time, 'nns_org_id' => $category_value['nns_org_id'], 'nns_category_id' => $category_value['nns_id'], 'nns_category_name' => $category_value['nns_name'], 'nns_pinyin' => $vod_name['nns_pinyin'], 'nns_video_type' => 0, );
						$re = nl_db_insert($db_w, 'nns_mgtvbk_category_content', $data);
						if ($re === false) {
							return false;
						}
					} else {//如果存在，则修改
						$vod_name = nl_db_get_one("select nns_name,nns_pinyin from nns_vod where nns_id='{$new_video_id}'", $db);
						$data = array('nns_video_id' => $new_video_id, 'nns_video_name' => $vod_name['nns_name'], 'nns_create_time' => $now_time, 'nns_modify_time' => $now_time, 'nns_org_id' => $category_value['nns_org_id'], 'nns_category_id' => $category_value['nns_id'], 'nns_category_name' => $category_value['nns_name'], 'nns_pinyin' => $vod_name['nns_pinyin'], 'nns_video_type' => 0, );
						$where = "nns_id='{$result['nns_id']}'";
						$re = nl_db_update($db_w, 'nns_mgtvbk_category_content', $data, $where);
						if ($re === false) {
							return false;
						}
					}
				}
			}elseif ($type == 'live') {
				foreach ($category as $category_value) {
					$sql = "select * from nns_mgtvbk_category_content where nns_org_id='" . $category_value['nns_org_id'] . "' and nns_video_id='" . $new_video_id . "' and nns_category_id='{$category_value['nns_id']}'  and  nns_video_type='1'";
					//die($sql);
					$result = nl_db_get_one($sql, $db);
					if (empty($result)) {//如果不存在，则添加
						$vod_name = nl_db_get_one("select nns_name,nns_pinyin from nns_live where nns_id='{$new_video_id}'", $db);
						$id = np_guid_rand();
						$data = array('nns_id' => $id, 'nns_video_id' => $new_video_id, 'nns_video_name' => $vod_name['nns_name'], 'nns_create_time' => $now_time, 'nns_modify_time' => $now_time, 'nns_org_id' => $category_value['nns_org_id'], 'nns_category_id' => $category_value['nns_id'], 'nns_category_name' => $category_value['nns_name'], 'nns_pinyin' => $vod_name['nns_pinyin'], 'nns_video_type' => 1, );
						$re = nl_db_insert($db_w, 'nns_mgtvbk_category_content', $data);
						if ($re === false) {
							return false;
						}
					} else {//如果存在，则修改
						$vod_name = nl_db_get_one("select nns_name,nns_pinyin from nns_live where nns_id='{$new_video_id}'", $db);
						$data = array('nns_video_id' => $new_video_id, 'nns_video_name' => $vod_name['nns_name'], 'nns_create_time' => $now_time, 'nns_modify_time' => $now_time, 'nns_org_id' => $category_value['nns_org_id'], 'nns_category_id' => $category_value['nns_id'], 'nns_category_name' => $category_value['nns_name'], 'nns_pinyin' => $vod_name['nns_pinyin'], 'nns_video_type' => 1, );
						$where = "nns_id='{$result['nns_id']}'";
						$re = nl_db_update($db_w, 'nns_mgtvbk_category_content', $data, $where);
						if ($re === false) {
							return false;
						}
					}
				}
			}			
		}
		return true;
	}
	
	
	
	

	/**
	 * $category 栏目
	 * $new_video_id 视频ID
	 * $type (vod,index,live,playbill)
	 */
	static public function _task_c2($category, $new_video_id, $type, $now_sp_id = false) {
		$result = array();

		$db_w = nn_get_db(NL_DB_WRITE);
		$db = nn_get_db(NL_DB_READ);

		if ($now_sp_id) {
			$sp_org_id = $now_sp_id;
		} else {
			$sp_org_id = SP_ORG_ID;
		}

		$src = "";
		$nns_name = '';
		$nns_ref_id_now = '';
		if ($type == 'index') {//分集
			$vod_index_tmp = nl_db_get_one("select nns_name,nns_vod_id,nns_action,nns_id from nns_vod_index where nns_id='{$new_video_id}'", $db);
			$nns_name = $vod_index_tmp['nns_name'];
			$src = $vod_index_tmp['nns_vod_id'];
			$action = $vod_index_tmp['nns_action'];
			$nns_ref_id_now = $vod_index_tmp['nns_id'];
		} elseif ($type == 'vod') {//影片
			$vod_tmp = nl_db_get_one("select nns_name,nns_action,nns_id from nns_vod where nns_id='{$new_video_id}'", $db);
			$nns_name = $vod_tmp['nns_name'];
			$action = $vod_tmp['nns_action'];
			$nns_ref_id_now = $vod_tmp['nns_id'];
		} elseif ($type == 'playbill') {//节目单
			$playbill_tmp = nl_db_get_one("select nns_name,nns_live_id,nns_id from nns_live_playbill_item where nns_id='{$new_video_id}'", $db);
			$nns_name = $playbill_tmp['nns_name'];
			$src = $playbill_tmp['nns_live_id'];
			$nns_ref_id_now = $playbill_tmp['nns_id'];
			unset($playbill_tmp);
		} elseif ($type == 'live') {
			$live_tmp = nl_db_get_one("select nns_name,nns_id from nns_live where nns_id='{$new_video_id}'", $db);
			$nns_name = $live_tmp['nns_name'];
			$nns_ref_id_now = $live_tmp['nns_id'];
		}
		$id = np_guid_rand();
		$now_time = date('Y-m-d H:i:s');
		$sql = "select * from nns_mgtvbk_c2_task where nns_type='{$type}' and 
					nns_ref_id='{$new_video_id}'  and nns_org_id='" . $sp_org_id . "'";
		$result = nl_db_get_one($sql, $db);
		
		if (is_array($result) && !empty($result)) {
			//判断注入状态
			if ($action == 'add') {
				if ($result['nns_status'] == -1 && $result['nns_action'] == 'add') {
					//注入失败，重新注入
					$sql = "update nns_mgtvbk_c2_task set nns_status=1,nns_modify_time='$now_time' where nns_id='{$result['nns_id']}'";
					$re = nl_execute_by_db($sql, $db_w);
					unset($sql);
					if ($re === false) {
						$data = array('nns_type' => $type, 'nns_name' => $nns_name, 'nns_ref_id' => $nns_ref_id_now, 'nns_org_id' => $sp_org_id, 'nns_action' => $action, );
						self::fail_content($data);
					}
				}
			} elseif ($action == 'modify') {
				if ($result['nns_status'] == -1 && $result['nns_action'] == 'add') {
					//注入失败，重新注入
					$sql = "update nns_mgtvbk_c2_task set nns_status=1,nns_modify_time='$now_time' where nns_id='{$result['nns_id']}'";
					$re = nl_execute_by_db($sql, $db_w);
					unset($sql);
					if ($re === false) {
						$data = array('nns_type' => $type, 'nns_name' => $nns_name, 'nns_ref_id' => $nns_ref_id_now, 'nns_org_id' => $sp_org_id, 'nns_action' => $action, );
						self::fail_content($data);
					}
				} else {
					if ($result['nns_status'] == 1 && $result['nns_action'] == 'add') {
						$nns_action = 'add';
						$nns_status = 1;
					} elseif ($result['nns_status'] == 0 && $result['nns_action'] == 'add') {
						$nns_action = 'modify';
						$nns_status = 2;
					} elseif (($result['nns_status'] == 2 || $result['nns_status'] == 0) && $result['nns_action'] == 'modify') {
						$nns_action = 'modify';
						$nns_status = 2;
					} elseif ($result['nns_status'] == 3 && $result['nns_action'] == 'destroy') {
						$nns_action = 'modify';
						$nns_status = 2;
					} elseif ($result['nns_status'] == 0 && $result['nns_action'] == 'destroy') {
						$nns_action = 'add';
						$nns_status = 1;
					}
					if ($result['nns_status'] == 0 || $result['nns_status'] == 1 || $result['nns_status'] == 2 || $result['nns_status'] == 3) {
						$sql = "update nns_mgtvbk_c2_task set nns_action='$nns_action' , 
									nns_status=$nns_status ,nns_modify_time='$now_time' where nns_id='{$result['nns_id']}'";
						if ($type == 'vod' || $type == 'index') {
							$sql = "update nns_mgtvbk_c2_task set nns_action='$nns_action' , 
										nns_status=$nns_status ,nns_modify_time='$now_time'  where  nns_type='vod' and 
									nns_ref_id='{$new_video_id}' and nns_org_id='" . $sp_org_id . "'";
						} elseif ($type == 'live' || $type == 'playbill') {
							$sql = "update nns_mgtvbk_c2_task set nns_action='$nns_action' , 
										nns_status=$nns_status ,nns_modify_time='$now_time'  where  nns_type='live' and 
									nns_ref_id='{$new_video_id}' and nns_org_id='" . $sp_org_id . "'";
						}
						$re = nl_execute_by_db($sql, $db_w);
						unset($sql);
						if ($re === false) {
							$data = array('nns_type' => $type, 'nns_name' => $nns_name, 'nns_ref_id' => $nns_ref_id_now, 'nns_org_id' => $sp_org_id, 'nns_action' => $action, );
							self::fail_content($data);
						}
					}
				}
			} elseif ($action == 'destroy') {
				$re = nl_execute_by_db("update nns_mgtvbk_c2_task set nns_name='" . $nns_name . "',nns_status=3,nns_action='destroy' where nns_id='" . $result['nns_id'] . "'", $db_w);
				if ($re === false) {
					$data = array('nns_type' => $type, 'nns_name' => $nns_name, 'nns_ref_id' => $nns_ref_id_now, 'nns_org_id' => $sp_org_id, 'nns_action' => $action, );
					self::fail_content($data);
				}
			}
		} else {
			if ($action == 'modify' || $action == 'add') {

				$data = array('nns_id' => $id, 'nns_type' => $type, 'nns_name' => $nns_name, 'nns_ref_id' => $new_video_id, 'nns_action' => 'add', 'nns_status' => 1, 'nns_create_time' => $now_time, 'nns_modify_time' => $now_time, 'nns_org_id' => $sp_org_id, 'nns_src_id' => $src, );
				$re = nl_db_insert($db_w, 'nns_mgtvbk_c2_task', $data);
				if ($re === false) {
					$data = array('nns_type' => $type, 'nns_name' => $nns_name, 'nns_ref_id' => $nns_ref_id_now, 'nns_org_id' => $sp_org_id, 'nns_action' => $action, );
					self::fail_content($data);
				}
			}
		}

		if ($type == 'vod') {
			foreach ($category as $category_value) {
				$sql = "select * from nns_mgtvbk_category_content where nns_org_id='" . $sp_org_id . "' and nns_video_id='" . $new_video_id . "' and nns_category_id='{$category_value['nns_id']}'  and  nns_video_type='0'";
				//die($sql);
				$result = nl_db_get_one($sql, $db);
				if (empty($result)) {//如果不存在，则添加
					$vod_name = nl_db_get_one("select nns_name,nns_pinyin from nns_vod where nns_id='{$new_video_id}'", $db);
					$id = np_guid_rand();
					$data = array('nns_id' => $id, 'nns_video_id' => $new_video_id, 'nns_video_name' => $vod_name['nns_name'], 'nns_create_time' => $now_time, 'nns_modify_time' => $now_time, 'nns_org_id' => $category_value['nns_org_id'], 'nns_category_id' => $category_value['nns_id'], 'nns_category_name' => $category_value['nns_name'], 'nns_pinyin' => $vod_name['nns_pinyin'], 'nns_video_type' => 0, );
					$re = nl_db_insert($db_w, 'nns_mgtvbk_category_content', $data);
					if ($re === false) {
						$data = array('nns_type' => $type, 'nns_name' => $nns_name, 'nns_ref_id' => $nns_ref_id_now, 'nns_org_id' => $sp_org_id, 'nns_action' => $action, );
						self::fail_content($data);
					}
				} else {//如果存在，则修改
					$vod_name = nl_db_get_one("select nns_name,nns_pinyin from nns_vod where nns_id='{$new_video_id}'", $db);
					$data = array('nns_video_id' => $new_video_id, 'nns_video_name' => $vod_name['nns_name'], 'nns_create_time' => $now_time, 'nns_modify_time' => $now_time, 'nns_org_id' => $category_value['nns_org_id'], 'nns_category_id' => $category_value['nns_id'], 'nns_category_name' => $category_value['nns_name'], 'nns_pinyin' => $vod_name['nns_pinyin'], 'nns_video_type' => 0, );
					$where = "nns_id='{$result['nns_id']}'";
					$re = nl_db_update($db_w, 'nns_mgtvbk_category_content', $data, $where);
					if ($re === false) {
						$data = array('nns_type' => $type, 'nns_name' => $nns_name, 'nns_ref_id' => $nns_ref_id_now, 'nns_org_id' => $sp_org_id, 'nns_action' => $action, );
						self::fail_content($data);
					}
				}
			}
		} elseif ($type == 'live') {
			foreach ($category as $category_value) {
				$sql = "select * from nns_mgtvbk_category_content where nns_org_id='" . $sp_org_id . "' and nns_video_id='" . $new_video_id . "' and nns_category_id='{$category_value['nns_id']}'  and  nns_video_type='1'";
				//die($sql);
				$result = nl_db_get_one($sql, $db);
				if (empty($result)) {//如果不存在，则添加
					$vod_name = nl_db_get_one("select nns_name,nns_pinyin from nns_live where nns_id='{$new_video_id}'", $db);
					$id = np_guid_rand();
					$data = array('nns_id' => $id, 'nns_video_id' => $new_video_id, 'nns_video_name' => $vod_name['nns_name'], 'nns_create_time' => $now_time, 'nns_modify_time' => $now_time, 'nns_org_id' => $category_value['nns_org_id'], 'nns_category_id' => $category_value['nns_id'], 'nns_category_name' => $category_value['nns_name'], 'nns_pinyin' => $vod_name['nns_pinyin'], 'nns_video_type' => 1, );
					$re = nl_db_insert($db_w, 'nns_mgtvbk_category_content', $data);
					if ($re === false) {
						$data = array('nns_type' => $type, 'nns_name' => $nns_name, 'nns_ref_id' => $nns_ref_id_now, 'nns_org_id' => $sp_org_id, 'nns_action' => $action, );
						self::fail_content($data);
					}
				} else {//如果存在，则修改
					$vod_name = nl_db_get_one("select nns_name,nns_pinyin from nns_live where nns_id='{$new_video_id}'", $db);
					$data = array('nns_video_id' => $new_video_id, 'nns_video_name' => $vod_name['nns_name'], 'nns_create_time' => $now_time, 'nns_modify_time' => $now_time, 'nns_org_id' => $category_value['nns_org_id'], 'nns_category_id' => $category_value['nns_id'], 'nns_category_name' => $category_value['nns_name'], 'nns_pinyin' => $vod_name['nns_pinyin'], 'nns_video_type' => 1, );
					$where = "nns_id='{$result['nns_id']}'";
					$re = nl_db_update($db_w, 'nns_mgtvbk_category_content', $data, $where);
					if ($re === false) {
						$data = array('nns_type' => $type, 'nns_name' => $nns_name, 'nns_ref_id' => $nns_ref_id_now, 'nns_org_id' => $sp_org_id, 'nns_action' => $action, );
						self::fail_content($data);
					}
				}
			}
		}
		return true;
	}

	/**
	 * $category 栏目
	 * $new_video_id 视频ID
	 * $type (vod,index,live,playbill)
	 */
	static public function _task_c2_del($category, $new_video_id, $type) {
		$db_w = nn_get_db(NL_DB_WRITE);
		$db = nn_get_db(NL_DB_READ);

		$id = np_guid_rand();
		$now_time = date('Y-m-d H:i:s');
		//$sql = "select * from nns_mgtvbk_c2_task where nns_type='{$type}' and
		//			nns_ref_id='{$new_video_id}' and nns_org_id='{$category['nns_org_id']}'
		//			and nns_category_id='{$category['nns_sp_category_id']}'";
		$sql = "select * from nns_mgtvbk_c2_task where nns_type='{$type}' and 
					nns_ref_id='{$new_video_id}' and nns_org_id='" . SP_ORG_ID . "'";
		$result = nl_db_get_one($sql, $db);
		if ($type == 'vod') {
			foreach ($category as $category_value) {
				$sql = "delete from nns_mgtvbk_category_content where nns_org_id='" . SP_ORG_ID . "' and nns_video_id='" . $new_video_id . "' and nns_category_id='{$category_value['nns_id']}'  and  nns_video_type='0'";
				nl_execute_by_db($sql, $db_w);
			}
		}

		if (is_array($result) && !empty($result)) {
			$sql = "update nns_mgtvbk_c2_task set nns_action='destroy' , 
						nns_status=3 ,nns_modify_time='$now_time' where nns_id='{$result['nns_id']}'";
			return nl_execute_by_db($sql, $db_w);
		}
	}

	/**
	 * 查看任务执行的状态
	 */
	static public function get_task_status($nns_id, $type, $org_id, $nns_video_type = 0) {
		$db = nn_get_db(NL_DB_READ);
		if ($nns_video_type == 0) {
			$nns_type = "index";
			$sql = "select * from nns_mgtvbk_c2_task 
				where nns_src_id='$nns_id' and  nns_type='$nns_type' 
				and nns_org_id='$org_id'";
			$task_all = nl_db_get_all($sql, $db);
			unset($sql);
			$sql = "select count(*) from nns_mgtvbk_c2_task 
				where nns_src_id='$nns_id' and  nns_type='$nns_type' 
				and nns_org_id='$org_id'  and nns_status=0";
			$num4 = nl_db_get_col($sql, $db);
			unset($sql);
			$sql = "select count(*) from nns_mgtvbk_c2_task 
				where nns_src_id='$nns_id' and  nns_type='$nns_type' 
				and nns_org_id='$org_id'  and nns_status>0";
			$num5 = nl_db_get_col($sql, $db);
			unset($sql);
			$str = "";
			$all_num = intval($num4 + $num5);
			if ($num4 == $all_num) {
				$str .= "<font color=green>注入成功</font>";
			} elseif ($num4 < $all_num) {
				$str .= "<font>等待注入</font>";
			}
		} else {
			$nns_type = "live";
			$sql = "select count(*) from nns_mgtvbk_c2_task 
				where nns_ref_id='$nns_id' and  nns_type='$nns_type' 
				and nns_org_id='$org_id'  and nns_status=0";
			$num4 = nl_db_get_col($sql, $db);
			unset($sql);

			if ($num4 >= 1) {
				$str .= "<font color=green>注入成功</font>";
			} else {
				$str .= "<font>等待注入</font>";
			}
		}

		return $str;
	}

	static public function exe_cmd($nns_id, $type, $cmd, $org_id) {
		$db_w = nn_get_db(NL_DB_WRITE);
		$db = nn_get_db(NL_DB_READ);
		if ($cmd == 'stop') {
			if ($type == 'vod') {//电影
				$sql = "update  nns_mgtvbk_c2_task  set nns_status ='0'  where nns_src_id='$nns_id' and  nns_type='index' and nns_org_id='$org_id'";
				$re = nl_execute_by_db($sql, $db_w);
				unset($sql);
			} elseif ($type == 'index') {//分集
				$sql = "update  nns_mgtvbk_c2_task set  nns_status ='0' where nns_ref_id='$nns_id' and  nns_type='index' and nns_org_id='$org_id'";
				$re = nl_execute_by_db($sql, $db_w);
				unset($sql);
			} elseif ($type == 'live') {//直播
				$sql = "update  nns_mgtvbk_c2_task  set nns_status ='0'  where nns_src_id='$nns_id' and  nns_type='live' and nns_org_id='$org_id'";
				$re = nl_execute_by_db($sql, $db_w);
				unset($sql);
			} elseif ($type == 'playbill') {//节目单
				$sql = "update  nns_mgtvbk_c2_task  set nns_status ='0'  where nns_ref_id='$nns_id' and  nns_type='playbill' and nns_org_id='$org_id'";
				$re = nl_execute_by_db($sql, $db_w);
				unset($sql);
			}
			if ($re) {
				$str = "成功";
			} else {
				$str = "失败";
			}
			return $str;
		} elseif ($cmd == 'restart') {
			if ($type == 'vod') {
				$nns_type1 = "vod";
				$nns_type2 = "index";
			} elseif ($type == 'index') {
				$nns_type1 = "vod";
				$nns_type2 = "index";
			} elseif ($type == 'live') {
				$nns_type1 = "live";
			} elseif ($type == 'playbill') {
				$nns_type1 = "playbill";
			}
			$sql = "select nns_id,nns_status,nns_action from nns_mgtvbk_c2_task where  nns_ref_id='$nns_id' and  nns_type='$nns_type1' 
				and nns_org_id='$org_id'";
			$task_all = nl_db_get_one($sql, $db);
			unset($sql);

			if ($task_all['nns_action'] == 'add') {
				$status = 1;
			} elseif ($task_all['nns_action'] == 'modify') {
				$status = 2;
			} elseif ($task_all['nns_action'] == 'destroy') {
				$status = 3;
			}
			nl_execute_by_db("update nns_mgtvbk_c2_task set nns_status='$status' where nns_id='{$task_all['nns_id']}'", $db_w);

			//content_task_model::vod($task_all['nns_id']);

			$array = array();
			$array[] = $task_all['nns_id'];
			unset($task_all);

			if ($type == 'vod' || $type == 'index') {
				$sql = "select nns_id,nns_status,nns_action from nns_mgtvbk_c2_task where  nns_src_id='$nns_id' and  nns_type='$nns_type2' 
					and nns_org_id='$org_id'";
				$task_all = nl_db_get_all($sql, $db);
				unset($sql);

				foreach ($task_all as $val) {
					if ($val['nns_action'] == 'add') {
						$status = 1;
					} elseif ($val['nns_action'] == 'modify') {
						$status = 2;
					} elseif ($val['nns_action'] == 'destroy') {
						$status = 3;
					}
					$array[] = $val['nns_id'];
					nl_execute_by_db("update nns_mgtvbk_c2_task set nns_status='$status' where nns_id='{$val['nns_id']}'", $db_w);
				}
			}
			$state = 1;
			foreach ($array as $value) {
				//添加日志
				$log_summary = '重新注入【';
				$log_summary .= nl_db_get_col("select nns_name from nns_mgtvbk_c2_task where nns_id='$value'", $db);
				log_model::log_write($log_summary . "】");
				$re = content_task_model::vod($value);

				if ($re) {
					$state = 1;
				} else {
					$state = 0;
					break;
				}
			}
			return "操作成功";

		}
	}

	/**
	 static public function get_clip_task_id($nns_id,$index_id,$org_id){
	 $db = nn_get_db(NL_DB_READ);

	 $sql = "select nns_id from nns_mgtvbk_clip_task where nns_org_id='$org_id' and nns_video_id='$nns_id' and nns_video_index_id='$index_id'";
	 $nns_id = nl_db_get_col($sql, $db);unset($sql);
	 return $nns_id;
	 }
	 ***/

	/*获取状态*/
	static public function get_status($nns_id) {
		$db = nn_get_db(NL_DB_READ);
		$str = "";
		$state = 0;
		$sql = "select * from nns_mgtvbk_c2_task where nns_id='$nns_id'";
		$re = nl_db_get_one($sql, $db);
		unset($sql);

		if ($re['nns_type'] == 'index') {
			$sql = "select count(*) from nns_vod_index where nns_vod_id='{$re['nns_src_id']}'";
			$re['nns_all_index'] = nl_db_get_col($sql, $db);
			unset($sql);
		} elseif ($re['nns_type'] == 'vod') {
			$sql = "select count(*) from nns_vod_index where nns_vod_id='{$re['nns_ref_id']}'";
			$re['nns_all_index'] = nl_db_get_col($sql, $db);
			unset($sql);
		}

		if ($re['nns_all_index'] == 1) {//电影
			$sql = "select nns_status from nns_mgtvbk_c2_task where nns_id='$nns_id'";
			$status = nl_db_get_col($sql, $db);
			unset($sql);
			if ($status > 0) {
				$str .= "电影基本信息未同步";
				$state = 1;
				return array('str' => $str, 'state' => $state);
			} else {
				$str .= "电影基本信息已同步,";
				$state = 1;
			}

		} elseif ($re['nns_all_index'] > 1) {//电视剧
			$sql = "select nns_status from nns_mgtvbk_c2_task where nns_id='$nns_id'" . "and nns_org_id='{$re['nns_org_id']}'";
			$status = nl_db_get_col($sql, $db);
			unset($sql);
			if ($status > 0) {
				$str .= "连续剧剧集基本信息未同步";
				$state = 1;
				return array('str' => $str, 'state' => $state);
			} else {
				$str .= "连续剧剧集基本信息已同步,";
				$state = 1;
			}
			//return array('str'=>$str,'state'=>$state);
		}
		//查看切片任务
		$sql = "select * from nns_mgtvbk_clip_task where nns_org_id='{$re['nns_org_id']}' and " . "nns_video_id='{$re['nns_src_id']}' and nns_video_index_id='{$re['nns_ref_id']}' and nns_video_type=0 order by nns_create_time desc limit 1";
		$clip = nl_db_get_one($sql, $db);
		unset($sql);
		if (is_array($clip)) {//存在
			$nns_state = $clip['nns_state'];
			if (empty($nns_state)) {
				$str .= "未执行切片任务";
			} elseif ($nns_state == 'c2_ok') {
				$str .= "切片完成";
			} elseif ($nns_state == 'handle') {
				$str .= "切片任务已下发";
			} elseif ($nns_state == 'download_fail') {
				$str .= "下载片源失败";
			} elseif ($nns_state == 'download_succ') {
				$str .= "下载片源成功";
			} elseif ($nns_state == 'cliping') {
				$str .= "正在切片";
			} elseif ($nns_state == 'cancel') {
				$str .= "切片任务取消";
			} elseif ($nns_state == 'c2_fail') {
				$str .= "切片失败";
			} elseif ($nns_state == 'clip_exists') {
				$str .= "切片存在";
			} elseif ($nns_state == 'unkown') {
				$str .= "未知错误";
			}
			$state = 2;
		} else {
			$str .= "没有生成切片任务";
			$state = 3;
		}
		return array('str' => $str, 'state' => $state);
	}

	static public function move_vod_arr($params) {
		$vod = explode(',', $_GET['nns_id']);
		$vad = array_filter($vod);
		foreach ($vad as $value) {
			$params['nns_id'] = $value;
			self::move_vod($params);
		}
		return 1;
	}

	/**
	 * $params = array(
	 'sp_id'=>$_GET['sp_id'],
	 'nns_id'=>$_GET['nns_id'],
	 'category_id'=>$_GET['category_id'],
	 'category_name'=>$_GET['category_name'],
	 );
	 */
	static public function move_vod($params) {
		$db = nn_get_db(NL_DB_WRITE);
		$db_r = nn_get_db(NL_DB_READ);
		$sql = "select * from nns_mgtvbk_category_content where nns_id in ('{$params['nns_id']}')";
		$result = nl_db_get_one($sql, $db);
		unset($sql);
		$id = np_guid_rand();
		$now_time = date('Y-m-d H:i:s');
		$sql = "insert into nns_mgtvbk_category_content (nns_id,nns_org_id,nns_category_id,nns_category_name,nns_video_id,nns_video_name,nns_create_time,nns_modify_time,nns_all_index,nns_pinyin,nns_video_type)" . "values ('{$id}','{$params['sp_id']}','{$params['category_id']}','{$params['category_name']}','{$result['nns_video_id']}','{$result['nns_video_name']}','{$now_time}','{$now_time}','{$result['nns_all_index']}','{$result['nns_pinyin']}','{$result['nns_video_type']}')";
		$re = nl_execute_by_db($sql, $db);
		unset($sql);

		//操作日志
		$log_summary = "复制【" . $result['nns_video_name'] . "】到【" . $params['category_name'] . "】栏目";
		log_model::log_write($log_summary);

		//当复制之后马上执行信息同步
		$vod_id = $result['nns_video_id'];
		$sp_id = $params['sp_id'];

		$sql = "select * from nns_mgtvbk_c2_task where nns_type='vod' and nns_org_id='$sp_id' and nns_ref_id='$vod_id'";
		$result_c2_task = nl_db_get_all($sql, $db);
		unset($sql);
		foreach ($result_c2_task as $value) {
			if ($value['nns_action'] == 'add' && $value['nns_action'] == 1) {
				$nns_action = 'add';
				$nns_status = 1;
			} elseif ($value['nns_action'] == 'add' && $value['nns_action'] == 0) {
				$nns_action = 'modify';
				$nns_status = 2;
			} elseif ($value['nns_action'] == 'modify' && ($value['nns_action'] == 0 || $value['nns_action'] == 2)) {
				$nns_action = 'modify';
				$nns_status = 2;
			} elseif ($value['nns_action'] == 'destroy' && $value['nns_action'] == 0) {
				$nns_action = 'add';
				$nns_status = 1;
			} elseif ($value['nns_action'] == 'destroy' && $value['nns_action'] == 3) {
				$nns_action = 'modify';
				$nns_status = 2;
			} elseif ($value['nns_action'] == -1) {
				if ($value['nns_action'] == 'add') {
					$nns_action = 'add';
					$nns_status = 1;
				} elseif ($value['nns_action'] == 'modify') {
					$nns_action = 'modify';
					$nns_status = 2;
				} elseif ($value['nns_action'] == 'destroy') {
					$nns_action = 'modify';
					$nns_status = 2;
				}
			}
			$sql = "update nns_mgtvbk_c2_task set nns_action='$nns_action',nns_status=$nns_status where nns_id='{$value['nns_id']}'";
			nl_execute_by_db($sql, $db);
			unset($sql);
			content_task_model::vod($value['nns_id']);
		}
		unset($result_c2_task);

		$sql = "select * from nns_mgtvbk_c2_task where nns_type='index' and nns_org_id='$sp_id' and nns_src_id='$vod_id'";
		$result_c2_task = nl_db_get_all($sql, $db);
		unset($sql);
		foreach ($result_c2_task as $value) {

			if ($value['nns_action'] == 'add' && $value['nns_action'] == 1) {
				$nns_action = 'add';
				$nns_status = 1;
			} elseif ($value['nns_action'] == 'add' && $value['nns_action'] == 0) {
				$nns_action = 'modify';
				$nns_status = 2;
			} elseif ($value['nns_action'] == 'modify' && ($value['nns_action'] == 0 || $value['nns_action'] == 2)) {
				$nns_action = 'modify';
				$nns_status = 2;
			} elseif ($value['nns_action'] == 'destroy' && $value['nns_action'] == 0) {
				$nns_action = 'add';
				$nns_status = 1;
			} elseif ($value['nns_action'] == 'destroy' && $value['nns_action'] == 3) {
				$nns_action = 'modify';
				$nns_status = 2;
			} elseif ($value['nns_action'] == -1) {
				if ($value['nns_action'] == 'add') {
					$nns_action = 'add';
					$nns_status = 1;
				} elseif ($value['nns_action'] == 'modify') {
					$nns_action = 'modify';
					$nns_status = 2;
				} elseif ($value['nns_action'] == 'destroy') {
					$nns_action = 'modify';
					$nns_status = 2;
				}
			}

			$sql = "update nns_mgtvbk_c2_task set nns_action='$nns_action',nns_status='$nns_status' where nns_id='{$value['nns_id']}'";
			nl_execute_by_db($sql, $db);
			unset($sql);
			content_task_model::vod($value['nns_id']);
		}
		unset($result_c2_task);

		if ($re) {
			return 1;
		} else {
			return 2;
		}
	}

	/*
	 * $params = array(
	 'sp_id'=>$_GET['sp_id'],
	 'nns_id'=>$_GET['nns_id']
	 );
	 */
	static public function delete_vod_content_all($params) {
		$nns_ids = explode(',', $params['nns_id']);
		foreach ($nns_ids as $value) {
			if ($value) {
				self::delete_vod_content(array('sp_id' => $params['sp_id'], 'nns_id' => $value));
			}
		}
		return true;
	}

	/*
	 * $params = array(
	 'sp_id'=>$_GET['sp_id'],
	 'nns_id'=>$_GET['nns_id']
	 );
	 */
	static public function delete_vod_content($params) {
		$db_r = nn_get_db(NL_DB_READ);
		$db_w = nn_get_db(NL_DB_WRITE);
		$sql = "select * from nns_mgtvbk_category_content where nns_id='{$params['nns_id']}'";
		$result1 = nl_db_get_one($sql, $db_r);
		unset($sql);
		if ($result1['nns_video_type'] == 0) {//点播
			$nns_type = 'vod';
		} else {//直播
			$nns_type = 'live';
		}
		$sql = "select * from nns_mgtvbk_c2_task where nns_org_id='" . $result1['nns_org_id'] . "' and nns_type='$nns_type' and nns_ref_id='" . $result1['nns_video_id'] . "' ";
		$c2_task = nl_db_get_one($sql, $db_r);
		if ($c2_task == false)
			return false;
		content_task_model::delete_task_vod($c2_task['nns_id'], $nns_type);
		$sql = "delete from  nns_mgtvbk_category_content where nns_id='{$params['nns_id']}'";
		$re = nl_execute_by_db($sql, $db_w);
		unset($sql);

		//操作日志
		$log_summary = "删除【" . $result1['nns_video_name'] . "】";
		log_model::log_write($log_summary);
		return true;
	}

	//栏目tree记录条数显示
	static public function get_count($sp_id) {
		$db_r = nn_get_db(NL_DB_READ);
		$sql = "select nns_category_id AS category_id,count(1) as num  from nns_mgtvbk_category_content where nns_org_id='{$sp_id}' group by nns_category_id";
		$data = nl_db_get_all($sql, $db_r);
		return array('data' => $data);
	}

	static public function get_playbill_status($id) {
		$db_r = nn_get_db(NL_DB_READ);
		$sql = "select * from nns_mgtvbk_c2_task where nns_org_id='" . SP_ORG_ID . "' and nns_ref_id='$id' and nns_type='playbill'";
		$data = nl_db_get_one($sql, $db_r);
		return $data;
	}

	/*
	 * @
	 */
	static public function get_content_log($sp_id, $filter, $offset, $limit,$state=false) {
		$db = nn_get_db(NL_DB_READ);
		$sql = "select SQL_CALC_FOUND_ROWS * " . "from nns_mgtvbk_category_content  where nns_org_id='$sp_id' and nns_video_type=0 ";
		if ($filter != null) {
			$wh_arr = array();
			if (!empty($filter['nns_name'])) {
				$wh_arr[] = " (nns_video_name like '%" . $filter['nns_name'] . "%'  or  nns_pinyin like '%" . $filter['nns_name'] . "%' )";
			}
			if (!empty($filter['day_picker_start'])) {
				$wh_arr[] = " nns_create_time>='" . $filter['day_picker_start'] . "'";
			}
			if (!empty($filter['day_picker_end'])) {
				$wh_arr[] = " nns_create_time<='" . $filter['day_picker_end'] . "'";
			}
			if (!empty($filter['nns_status'])) {
				$wh_arr[] = " nns_status='" . $filter['nns_status'] . "'";
			}

			if (!empty($filter['nns_category'])) {
				$wh_arr[] = " nns_category_id like '" . $filter['nns_category'] . "%'";
			}

			if (!empty($wh_arr))
				$sql .= " and " . implode(" and ", $wh_arr);
		}
		if($state){
			$sql .=" group by nns_video_id"; 
		}
		$sql .= " order by nns_create_time desc ";
		$sql .= " limit $offset,$limit";
		$data = nl_db_get_all($sql, $db);
		$count_sql = "SELECT FOUND_ROWS() ";
		$rows = nl_db_get_col($count_sql, $db);
		if (count($data > 0)) {
			foreach ($data as $key => $value) {
				$nns_all_index = nl_db_get_col("select nns_all_index from nns_vod where nns_id='{$value['nns_video_id']}'", $db);
				if (empty($nns_all_index)) {
					$nns_all_index = 0;
				}
				$data[$key]['nns_all_index'] = $nns_all_index;
			}
		}
		return array('data' => $data, 'rows' => $rows);
	}

	//统计
	static public function statistics() {
		$db = nn_get_db(NL_DB_READ);
		//芒果注入的影片
		$sql = "select count(nns_id) from nns_vod";
		$vod = nl_db_get_col($sql, $db);
		//注入华为成功的影片
		$sql = "select count(nns_id) from nns_mgtvbk_c2_task where nns_org_id='" . SP_ORG_ID . "' and nns_type='vod' and nns_status=0";
		$vod_huawei_success = nl_db_get_col($sql, $db);
		//等待注入华为
		$sql = "select count(nns_id) from nns_mgtvbk_c2_task where nns_org_id='" . SP_ORG_ID . "' and nns_type='vod' and nns_status in(1,2,3,5)";
		$vod_huawei_wait = nl_db_get_col($sql, $db);
		//注入华为失败
		$sql = "select count(nns_id) from nns_mgtvbk_c2_task where nns_org_id='" . SP_ORG_ID . "' and nns_type='vod' and nns_status in(-1,4)";
		$vod_huawei_failed = nl_db_get_col($sql, $db);
		//芒果注入的分集
		$sql = "select count(nns_id) from nns_vod_index";
		$vod_index = nl_db_get_col($sql, $db);

		//注入华为成功的分集
		$sql = "select count(nns_id) from nns_mgtvbk_c2_task where nns_org_id='" . SP_ORG_ID . "' and nns_type='index' and nns_status=0";
		$vod_index_huawei_success = nl_db_get_col($sql, $db);
		//等待注入华为
		$sql = "select count(nns_id) from nns_mgtvbk_c2_task where nns_org_id='" . SP_ORG_ID . "' and nns_type='index' and nns_status in(1,2,3,5)";
		$vod_index_huawei_wait = nl_db_get_col($sql, $db);
		//注入华为失败
		$sql = "select count(nns_id) from nns_mgtvbk_c2_task where nns_org_id='" . SP_ORG_ID . "' and nns_type='index' and nns_status in(-1,4)";
		$vod_index_huawei_failed = nl_db_get_col($sql, $db);
		//所有分片
		//$sql = "select count(nns_id) from nns_vod_media";
		$sql = "select  count(distinct nns_video_index_id)   from nns_mgtvbk_clip_task where  nns_org_id='" . SP_ORG_ID . "'";
		$vod_media = nl_db_get_col($sql, $db);
		//已经完成的切片
		$sql = "select  count(distinct nns_video_index_id)   from nns_mgtvbk_clip_task where nns_state='c2_ok' and nns_org_id='" . SP_ORG_ID . "'";
		$vod_media_clip_success = nl_db_get_col($sql, $db);

		$sql = "select  count(distinct nns_video_index_id)   from nns_mgtvbk_clip_task where  (nns_state!='cancel' or ISNULL(nns_state) or nns_state='') and nns_org_id='" . SP_ORG_ID . "' and (nns_state='' or nns_state is null)";
		$vod_media_clip_wait = nl_db_get_col($sql, $db);

		$sql = "select  count(distinct nns_video_index_id)   from nns_mgtvbk_clip_task where nns_state='c2_fail' and nns_org_id='" . SP_ORG_ID . "'";
		$vod_media_clip_failed = nl_db_get_col($sql, $db);

		/*
		 $sql = "select  count(distinct nns_video_index_id)   from nns_mgtvbk_clip_task where nns_state='c2_ok'";
		 $clip = nl_db_get_col($sql,$db);
		 $sql = "select count(nns_id) from nns_mgtvbk_c2_log where nns_org_id='".SP_ORG_ID."' and nns_task_type='Movie' and nns_action='REGIST' and nns_notify_result='0'";
		 $clip_import = nl_db_get_col($sql,$db);
		 $sql = "select count(nns_id) from nns_mgtvbk_clip_task where nns_org_id='".SP_ORG_ID."' and (nns_state is null or nns_state='')";
		 $clip_surplus = nl_db_get_col($sql,$db);

		 $sql = "select count(nns_id) from nns_mgtvbk_c2_task where nns_org_id='".SP_ORG_ID."' and nns_status=0";
		 $success = nl_db_get_col($sql,$db);

		 $sql = "select count(nns_id) from nns_mgtvbk_c2_task where nns_org_id='".SP_ORG_ID."' and nns_status in(1,2,3) ";
		 $wait = nl_db_get_col($sql,$db);

		 $sql = "select count(nns_id) from nns_mgtvbk_c2_task where nns_org_id='".SP_ORG_ID."' and nns_status=-1";
		 $failed = nl_db_get_col($sql,$db);
		 */
		return array('vod' => $vod, 'vod_huawei_success' => $vod_huawei_success, 'vod_huawei_wait' => $vod_huawei_wait, 'vod_huawei_failed' => $vod_huawei_failed, 'vod_index' => $vod_index, 'vod_index_huawei_success' => $vod_index_huawei_success, 'vod_index_huawei_wait' => $vod_index_huawei_wait, 'vod_index_huawei_failed' => $vod_index_huawei_failed, 'vod_media' => $vod_media, 'vod_media_clip_success' => $vod_media_clip_success, 'vod_media_clip_wait' => $vod_media_clip_wait, 'vod_media_clip_failed' => $vod_media_clip_failed
		/*
		 'clip_surplus'=>$clip_surplus,
		 'success'=>$success,
		 'wait'=>$wait,
		 'failed'=>$failed,*/
		);

	}

}
?>
